//
//  DeploymentDetailsRootController.m
//  jboss-admin
//
//  Created by Christos Vasilakis on 06/07/2010.
//  Copyright 2010 All rights reserved.
//

#import "DeploymentDetailsRootController.h"
#import "DeploymentDetailsSummaryController.h"
#import "DeploymentDetailsMetricsController.h"
#import "ProgressViewController.h"

@implementation DeploymentDetailsRootController

@synthesize results;
@synthesize spinner;
@synthesize index;
@synthesize tabBarController;

- (void)loadView {
	[super viewDidLoad];
	
    UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	contentView.backgroundColor = [UIColor whiteColor];
	self.view = contentView;
	[contentView release];
    
    // Declare all three view controllers
	DeploymentDetailsSummaryController *summaryController = [[DeploymentDetailsSummaryController alloc] initWithStyle:UITableViewStylePlain];
	DeploymentDetailsMetricsController *metricsController = [[DeploymentDetailsMetricsController alloc] initWithStyle:UITableViewStylePlain];
    //DeploymentDetailsOperationsController *operationsController = [[DeploymentDetailsOperationsController alloc] init];
	
	metricsController.results = self.results;
	metricsController.index = self.index;
		
    // Set a title for each view controller. These will also be names of each tab
    summaryController.title = @"Summary";
    metricsController.title = @"Metrics";
    //operationsController.title = @"Operations";

    summaryController.tabBarItem.image = [UIImage imageNamed:@"summary.png"];
	metricsController.tabBarItem.image = [UIImage imageNamed:@"metrics.png"];
    //operationsController.tabBarItem.image = [UIImage imageNamed:@"operations.png"];
        
    // Create an empty tab controller and set it to fill the screen minus the top title bar
    UITabBarController *atabBarController = [[UITabBarController alloc] init];
	atabBarController.delegate = self;
	atabBarController.view.frame = CGRectMake(0, 0, 320, 460);
    
	// Set each tab to show an appropriate view controller
    [atabBarController setViewControllers: [NSArray arrayWithObjects:summaryController, metricsController, /*operationsController*/ nil]];
    
	self.tabBarController = atabBarController;
	
    // Clean up objects we don't need anymore
    [summaryController release];
    [metricsController release];
	// [operationsController release];
	
	UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh 
																				   target:self
																				   action:@selector(refresh:)];
	self.navigationItem.rightBarButtonItem = refreshButton;
	[refreshButton release];
	
    // Finally, add the tab controller view to the parent view
    [self.view addSubview:tabBarController.view];
	
	NSLog(@"DeploymentDetailsRootController viewDidLoad");
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSLog(@"DeploymentDetailsRootController viewDidUnload");
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	[super viewDidUnload];
	
	self.results = nil;
	self.tabBarController = nil;
	self.spinner = nil;
	
	NSLog(@"DeploymentDetailsRootController viewDidUnload");
}


- (void)dealloc {
	[results release];
	[tabBarController release];
	[spinner release];

	NSLog(@"DeploymentDetailsRootController deAlloc");
	
    [super dealloc];
}

#pragma mark -
#pragma mark TabBar Delegate Methods
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

	/*
	if ( [viewController isKindOfClass:DeploymentDetailsOperationsController.class]) {
		self.navigationItem.rightBarButtonItem.enabled = NO;
	} else {
		self.navigationItem.rightBarButtonItem.enabled = YES;
	}
	 */

}

#pragma mark -
#pragma mark Action Calls
- (IBAction)refresh:(id)sender {
	id selectedController = tabBarController.selectedViewController;
	
	[selectedController refresh];
}

@end
